const mongoose = require('mongoose');
const dotenv = require('dotenv');

dotenv.config();

// 数据库连接选项
const options = {
  useNewUrlParser: true,
  useUnifiedTopology: true,
};

// 数据库连接函数
const connectDB = async () => {
  try {
    const conn = await mongoose.connect(process.env.MONGODB_URI, options);
    console.log(`MongoDB连接成功: ${conn.connection.host}`);
    return conn;
  } catch (error) {
    console.error(`MongoDB连接错误: ${error.message}`);
    process.exit(1);
  }
};

module.exports = connectDB;